<?
/*--|	classiHareket.php
  Software: uyguLama - hareket class 4 hareket app
   Version: 1.0
   Contact: http://uygulama.net
      Info: http://uygulama.net
   Support: http://uygulama.googlecode.com
--------------------------------------------------------------------------
    Author: Göker CEBECi (founder) http://goker.cebeci.name
--------------------------------------------------------------------------
   License: Distributed under the Lesser General Public License (LGPL)
            http://www.gnu.org/copyleft/lesser.html
 This program is distributed in the hope that it will be useful - WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE.
-------------------------------------------------------------------------*/
// status pandik,nah
class hareket {
	/**
	 * $memberOne < $memberTwo ? one : two
	 *
	 * @param int $memberOne
	 * @param int $memberTwo
	 * @return array
	 */
	private static function _order($memberOne, $memberTwo){
		return ($memberOne < $memberTwo)?array($memberOne,$memberTwo,'one'):array($memberTwo,$memberOne,'two');
	}
	private static function _add($memberOne, $memberTwo, $status = 'pandik'){
		if($memberOne == $memberTwo) return true;
		list($memberOne, $memberTwo, $sender) = self::_order($memberOne, $memberTwo);
		db::query(sprintf("INSERT INTO hareket (memberOne,memberTwo,sender,status,date)
			VALUES ('%d','%d','%s','%s',NOW())
			ON DUPLICATE KEY UPDATE sender = '%s', status = '%s', `date` = NOW();"
			,$memberOne,$memberTwo,$sender,$status,$sender,$status));
		return true;
	}
    private static function _get($a){
        if($a['list']){
        $r = db::query(sprintf("SELECT IF(memberOne='%s',memberTwo,memberOne) as ID, status FROM hareket WHERE 1=1",$a['UID'])
                .($a['ID']?sprintf(" AND ID = %d",$a['ID']):'')
            .($a['sender']?sprintf(" AND ((memberOne = %d AND sender = 'one') OR (memberTwo = %d AND sender = 'two'))",$a['UID'],$a['UID']):'')
            .($a['recipient']?sprintf(" AND ((memberOne = %d AND sender = 'two') OR (memberTwo = %d AND sender = 'one'))",$a['UID'],$a['UID']):'')
            .($a['match']?sprintf(" AND (memberOne = %d OR memberTwo = %d)",$a['UID'],$a['UID']):'')
            .($a['status']?sprintf(" AND status = '%s'",$a['status']):'')
            .($a['limit']?sprintf(" LIMIT %d",$a['limit']):'')
            .';',true,true);
            if($r) foreach ($r as $k=>$i){
                $r[$k] = profile::get(array('ID'=>$i->ID));
                $r[$k]->hareket = $i->status;
            }
        } elseif($a['count']){
        $r = db::query('SELECT COUNT(memberOne) as total FROM hareket WHERE 1=1'
            .($a['sender']?sprintf(" AND ((memberOne = %d AND sender = 'one') OR (memberTwo = %d AND sender = 'two'))",$a['UID'],$a['UID']):'')
            .($a['recipient']?sprintf(" AND ((memberOne = %d AND sender = 'two') OR (memberTwo = %d AND sender = 'one'))",$a['UID'],$a['UID']):'')
            .($a['match']?sprintf(" AND (memberOne = %d OR memberTwo = %d)",$a['UID'],$a['UID']):'')
            .($a['status']?sprintf(" AND status = '%s'",$a['status']):'')
            .';',true);
        $r = $r->total;
        }
        return $r;
    }
/* -----------------------------------------------------------------------
	PUBLIC FUNCTIONS
 ------------------------------------------------------------------------*/

/* -----------------------------------------------------------------------
	SPECIAL FUNCTIONS
 ------------------------------------------------------------------------*/
	/**
	 * cek function
	 *
	 * @param big_int $memberOne
	 * @param big_int $memberTwo
	 * @return object
	 */
	public static function cek($memberOne, $memberTwo, $status){
		self::_add($memberOne, $memberTwo, $status);
		return common::result(200);
	}
	/**
	 * sineyecek [delete record]
	 *
	 * @param big_int $memberOne
	 * @param big_int $memberTwo
	 * @return object
	 */
	public static function sineyecek($memberOne, $memberTwo){
		if($memberOne == $memberTwo) return common::result(600);
		list($memberOne, $memberTwo, $sender) = self::_order($memberOne, $memberTwo);
		db::query(sprintf("DELETE FROM hareket
			WHERE memberOne = '%d' AND memberTwo = '%d' LIMIT 1;",
			$memberOne,$memberTwo));
		return common::result(200);
	}
	/**
	 * people who like me list
	 *
	 * @param array $a
	 * @return object
	 */
	public static function hareketler($a){
		$a['recipient'] = true;
		return self::_get($a);
	}

    /**
     * check friend and friend request
     *
     * @param big_int $memberOne
     * @param big_int $memberTwo
     * @return object
     */
    public static function check($memberOne,$memberTwo){
        if($memberOne == $memberTwo) return true;
        list($memberOne, $memberTwo, $sender) = self::_order($memberOne, $memberTwo);
        $r = db::query(sprintf("SELECT sender,status FROM hareket
            WHERE memberOne = %d AND memberTwo = %d LIMIT 1;",
            $memberOne,$memberTwo),true);
        return $sender==$r->sender?'awaiting':$r->status;
    }
}
?>
